/*
 *
 * <p>ArticleListAction.java</p>
 *   
 * Created on 2009-7-28 by sunan
 *
 * $Log:$
 */

package cn.hs.blog.article.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.hs.blog.article.module.Article;
import cn.hs.blog.article.module.ArticleStatistics;
import cn.hs.blog.base.ActionUtils;
import cn.hs.blog.base.PMF;
import cn.hs.blog.base.PageLoader;
import cn.hs.blog.base.PageLoaderImpl;

public class ArticleAction {

	/**
     * 
     */
	private static final long serialVersionUID = 6291138606394081958L;

	public void list(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		System.out.println("ArticleListAction");
		try {
			req.getRequestDispatcher("/jsp/articleList.jsp").forward(req, resp);
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public void getArticles(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		System.out.println("getArticles");
		PageLoader<Article> page = new PageLoaderImpl<Article>();
		ActionUtils.populate(req, page);
		PersistenceManager pm = PMF.get().getPersistenceManager();
		String JDOQL = "select from " + Article.class.getName()
				+ " order by id ";

		System.out.println("JDOQL: " + JDOQL);
		Query query = pm.newQuery(JDOQL);
		// query.getFetchPlan()
		System.out.println("pageNum: " + page.getPageNum() + ", numPerPage: "
				+ page.getNumPerPage());
		query.setRange((page.getPageNum() - 1) * page.getNumPerPage(), page
				.getPageNum()
				* page.getNumPerPage() + 1);
		Object temp = query.execute();
		List<Article> articles = null;
		if (temp != null) {
			articles = (List<Article>) temp;
		}

		page.setIsEnd(articles.size() < 11);

		page.setResultList(articles);

		StringBuffer sb = new StringBuffer("");
		sb.append("<?xml version =\"1.0\" encoding=\"utf-8\"?>\n");
		sb.append("\n");

		sb.append("<rows>");
		sb.append("\n");

		sb.append("<page>" + page.getPageNum() + "</page>");
		sb.append("\n");

		sb.append("<total>1</total>");
		sb.append("\n");

		sb.append("<records>" + page.getPageNum() + "</records>");
		sb.append("\n");

		for (Article article : articles) {
			sb.append("<row id = \"" + article.getId() + "\">");
			sb.append("\n");

			sb.append("<cell>" + article.getId() + "</cell>");
			sb.append("\n");

			sb.append("<cell>" + article.getTitle() + "</cell>");
			sb.append("\n");

			sb.append("<cell>" + article.getAuthor() + "</cell>");
			sb.append("\n");

			sb.append("<cell>" + article.getDate() + "</cell>");
			sb.append("\n");

			sb.append("</row>");
			sb.append("\n");

		}

		sb.append("</rows>");
		sb.append("\n");

		System.out.println(sb);

		resp.setContentType("text/xml; charset=UTF-8");
		// ServletOutputStream out = resp.getOutputStream();

		PrintWriter out = resp.getWriter();
		out.print(sb.toString());
		out.close();
		// resp.getWriter().append(sb);

	}

	public void toAdd(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {

		System.out.println("ToAddArticleAction");
		try {
			req.getRequestDispatcher("/jsp/toAddArticle.jsp")
					.forward(req, resp);
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public void doAdd(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {

		System.out.println("DoAddArticleAction");

		Article article = new Article();
		ArticleStatistics aStatistics = new ArticleStatistics();

		ActionUtils.populate(req, article);
		// article.setTitle(req.getParameter("title"));
		// article.setContent(req.getParameter("content"));
		// article.setAuthor(req.getParameter("author"));
		Date toDay = new Date();
		article.setDate(toDay);
		article.setBeable("1");
		article.setCUser(Long.parseLong("1"));
		article.setCDate(toDay);
		article.setLuUser(Long.parseLong("1"));
		article.setLuDate(toDay);
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			pm.makePersistent(article);

			aStatistics.setArticleId(article.getId());
			aStatistics.setReadCount(Long.parseLong("0"));
			aStatistics.setReplyCount(Long.parseLong("0"));
			pm.makePersistent(aStatistics);

		} finally {
			pm.close();
		}

		// list(req, resp);
		resp.sendRedirect("/blog/article/Article.do?method=listForPage");

	}

	public void listForPage(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		System.out.println("ArticleListAction");
		PageLoader<Article> page = new PageLoaderImpl<Article>();
		ActionUtils.populate(req, page);
		PersistenceManager pm = PMF.get().getPersistenceManager();
		String JDOQL = "select from " + Article.class.getName()
				+ " order by id ";

		System.out.println("JDOQL: " + JDOQL);
		Query query = pm.newQuery(JDOQL);
		// query.getFetchPlan()
		System.out.println("pageNum: " + page.getPageNum() + ", numPerPage: "
				+ page.getNumPerPage());
		query.setRange((page.getPageNum() - 1) * page.getNumPerPage(), page
				.getPageNum()
				* page.getNumPerPage() + 1);
		Object temp = query.execute();
		List<Article> articles = null;
		if (temp != null) {
			articles = (List<Article>) temp;
		}

		page.setIsEnd(articles.size() < 11);

		page.setResultList(articles);

		req.setAttribute("page", page);
		try {
			req.getRequestDispatcher("/jsp/articleListForPage.jsp").forward(
					req, resp);
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}
